﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Tech4WPF.ChartControl;


namespace Tech4WPF_TestApp
{
    /// <summary>
    /// Interaction logic for ChartControlTest.xaml
    /// </summary>
    public partial class ChartControlTest : Window
    {
        #region Constructors

        public ChartControlTest()
        {
            InitializeComponent();

            cbxLegendPositionSetItems();
            cbxsStrokePatternSetItems();
            cbxsDrawTypeSetItems();
            cbxsSymbolTypeSetItems();

            AddData();
        }

        #endregion Constructors

        #region Properties

        System.Windows.Forms.ColorDialog colorPicker;
        /// <summary>
        /// Gets the color picker. (singleton)
        /// </summary>
        private System.Windows.Forms.ColorDialog ColorPicker
        {
            get
            {
                if (this.colorPicker == null)
                {
                    this.colorPicker = new System.Windows.Forms.ColorDialog();
                }
                return this.colorPicker;
            }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Set items for draw types combobox
        /// </summary>
        private void cbxsDrawTypeSetItems()
        {
            foreach (DrawType drawType in Enum.GetValues(typeof(DrawType)))
            {
                cbxSineDrawType.Items.Add(drawType);
                cbxCosineDrawType.Items.Add(drawType);
            }
        }

        /// <summary>
        /// Set items for symbol types combobox
        /// </summary>
        private void cbxsSymbolTypeSetItems()
        {
            foreach (SymbolType symbolType in Enum.GetValues(typeof(SymbolType)))
            {
                cbxSineSymbolType.Items.Add(symbolType);
                cbxCosineSymbolType.Items.Add(symbolType);
            }
        }

        /// <summary>
        /// Set items for stroke pattern combobox
        /// </summary>
        private void cbxsStrokePatternSetItems()
        {
            foreach (StrokePattern strokePattern in Enum.GetValues(typeof(StrokePattern)))
            {
                cbxSineStrokePattern.Items.Add(strokePattern);
                cbxCosineStrokePattern.Items.Add(strokePattern);
                cbxGridlinePattern.Items.Add(strokePattern);
            }
        }

        /// <summary>
        /// Set items for legend position combobox
        /// </summary>
        private void cbxLegendPositionSetItems()
        {
            foreach (LegendPosition legendPosition in Enum.GetValues(typeof(LegendPosition)))
            {
                cbxLegendPosition.Items.Add(legendPosition);
            }
        }

        /// <summary>
        /// Adds the data to chart.
        /// </summary>
        private void AddData()
        {
            DataSeries ds = new DataSeries();

            // Sine curve:
            ds = new DataSeries();
            ds.Name = "Sine";
            ds.ChartStyle.SymbolType = SymbolType.Bar;
            ds.ChartStyle.DrawType = DrawType.None;
            ds.ChartStyle.SymbolFill = Brushes.LightBlue.Clone();

            for (int i = 0; i < 70; i++)
            {
                double x = i / 5.0;
                double y = Math.Sin(x);
                ds.PointSeries.Add(new Point(x, y));
            }
            chartControl.DataCollection.Add(ds);
            setSineBindings(ds);

            // Cosine curve:
            ds = new DataSeries();
            ds.ChartStyle.Stroke = Brushes.Red;
            ds.Name = "Cosine";
            ds.ChartStyle.SymbolType = SymbolType.Circle;
            ds.ChartStyle.DrawType = DrawType.Polyline;

            for (int i = 0; i < 70; i++)
            {
                double x = i / 5.0;
                double y = Math.Cos(x);
                ds.PointSeries.Add(new Point(x, y));
            }
            chartControl.DataCollection.Add(ds);
            setCosineBindings(ds);

            chartControl.IsLegend = true;
        }

        /// <summary>
        /// Sets the sine bindings.
        /// </summary>
        /// <param name="ds">The ds.</param>
        private void setSineBindings(DataSeries ds)
        {
            Binding binding = new Binding();
            binding.Source = ds;
            binding.Path = new PropertyPath("Name");
            tbxSineName.SetBinding(TextBox.TextProperty, binding);

            binding = new Binding();
            binding.Source = ds;
            binding.Path = new PropertyPath("ChartStyle.DrawType");
            cbxSineDrawType.SetBinding(ComboBox.SelectedItemProperty, binding);

            binding = new Binding();
            binding.Source = ds;
            binding.Path = new PropertyPath("ChartStyle.SymbolType");
            cbxSineSymbolType.SetBinding(ComboBox.SelectedItemProperty, binding);

            binding = new Binding();
            binding.Source = ds;
            binding.Path = new PropertyPath("ChartStyle.StrokePattern");
            cbxSineStrokePattern.SetBinding(ComboBox.SelectedItemProperty, binding);

            binding = new Binding();
            binding.Source = ds;
            binding.Path = new PropertyPath("ChartStyle.StrokeThickness");
            tbxSineThickness.SetBinding(TextBox.TextProperty, binding);

            binding = new Binding();
            binding.Source = ds;
            binding.Path = new PropertyPath("ChartStyle.SymbolSize");
            tbxSineSymbolSize.SetBinding(TextBox.TextProperty, binding);
        }

        /// <summary>
        /// Sets the cosine bindings.
        /// </summary>
        /// <param name="ds">The ds.</param>
        private void setCosineBindings(DataSeries ds)
        {
            Binding binding = new Binding();
            binding.Source = ds;
            binding.Path = new PropertyPath("Name");
            tbxCosineName.SetBinding(TextBox.TextProperty, binding);

            binding = new Binding();
            binding.Source = ds;
            binding.Path = new PropertyPath("ChartStyle.DrawType");
            cbxCosineDrawType.SetBinding(ComboBox.SelectedItemProperty, binding);

            binding = new Binding();
            binding.Source = ds;
            binding.Path = new PropertyPath("ChartStyle.SymbolType");
            cbxCosineSymbolType.SetBinding(ComboBox.SelectedItemProperty, binding);

            binding = new Binding();
            binding.Source = ds;
            binding.Path = new PropertyPath("ChartStyle.StrokePattern");
            cbxCosineStrokePattern.SetBinding(ComboBox.SelectedItemProperty, binding);

            binding = new Binding();
            binding.Source = ds;
            binding.Path = new PropertyPath("ChartStyle.StrokeThickness");
            tbxCosineThickness.SetBinding(TextBox.TextProperty, binding);

            binding = new Binding();
            binding.Source = ds;
            binding.Path = new PropertyPath("ChartStyle.SymbolSize");
            tbxCosineSymbolSize.SetBinding(TextBox.TextProperty, binding);
        }

        /// <summary>
        /// Handles the Click event of the btnGridLineColor control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btnGridLineColorOnClick(object sender, RoutedEventArgs e)
        {
            this.chartControl.GridlineColor = chooseColorBrush();
        }

        /// <summary>
        /// Handles the Click event of the btnSineStroke control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btnSineStrokeOnClick(object sender, RoutedEventArgs e)
        {
            this.chartControl.DataCollection[0].ChartStyle.Stroke = chooseColorBrush();
        }

        /// <summary>
        /// Handles the Click event of the btnCosineStroke control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btnCosineStrokeOnClick(object sender, RoutedEventArgs e)
        {
            this.chartControl.DataCollection[1].ChartStyle.Stroke = chooseColorBrush();
        }

        /// <summary>
        /// Handles the Click event of the btnSineFill control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btnSineFillOnClick(object sender, RoutedEventArgs e)
        {
            Brush fill = chooseColorBrush();
            if (fill != null)
                fill.Opacity = 0.4;
            this.chartControl.DataCollection[0].ChartStyle.Fill = fill;
        }

        /// <summary>
        /// Handles the Click event of the btnCosineFill control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btnCosineFillOnClick(object sender, RoutedEventArgs e)
        {
            Brush fill = chooseColorBrush();
            if (fill != null)
                fill.Opacity = 0.4;
            this.chartControl.DataCollection[1].ChartStyle.Fill = fill;
        }

        /// <summary>
        /// Chooses the color brush.
        /// </summary>
        /// <returns>Selected Brush, when strno returns null</returns>
        private Brush chooseColorBrush()
        {
            if (this.ColorPicker.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                System.Drawing.Color color = this.ColorPicker.Color;
                return new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B));
            }
            return null;
        }

        /// <summary>
        /// Handles the Click event of the btnRefreshChart control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btnRefreshChartOnClick(object sender, RoutedEventArgs e)
        {
            this.chartControl.DataCollection = new List<DataSeries>(this.chartControl.DataCollection);
        }

        #endregion Methods
    }
}
